﻿using System;
using System.Net;
using System.Net.Sockets;
using HslCommunication.BasicFramework;
using HslCommunication.Core.Net;
using HslCommunication.LogNet;

namespace HslCommunication.Profinet.Omron
{
	/// <inheritdoc cref="T:HslCommunication.Profinet.Omron.OmronFinsServer" />
	// Token: 0x0200005E RID: 94
	public class OmronFinsUdpServer : OmronFinsServer
	{
		/// <summary>
		/// 实例化一个默认的对象<br />
		/// Instantiate a default object
		/// </summary>
		// Token: 0x060007D2 RID: 2002 RVA: 0x00029CF1 File Offset: 0x00027EF1
		public OmronFinsUdpServer()
		{
			this.headLength = 0;
		}

		/// <summary>
		/// 获取或设置一次接收时的数据长度，默认2KB数据长度
		/// </summary>
		// Token: 0x17000169 RID: 361
		// (get) Token: 0x060007D3 RID: 2003 RVA: 0x00029D0D File Offset: 0x00027F0D
		// (set) Token: 0x060007D4 RID: 2004 RVA: 0x00029D15 File Offset: 0x00027F15
		public int ReceiveCacheLength { get; set; } = 2048;

		/// <inheritdoc />
		// Token: 0x060007D5 RID: 2005 RVA: 0x00029D20 File Offset: 0x00027F20
		public override void ServerStart(int port)
		{
			bool flag = !base.IsStarted;
			if (flag)
			{
				this.StartInitialization();
				this.CoreSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
				this.CoreSocket.Bind(new IPEndPoint(IPAddress.Any, port));
				this.RefreshReceive();
				base.IsStarted = true;
				base.Port = port;
				ILogNet logNet = base.LogNet;
				if (logNet != null)
				{
					logNet.WriteInfo(this.ToString(), StringResources.Language.NetEngineStart);
				}
			}
		}

		/// <inheritdoc />
		// Token: 0x060007D6 RID: 2006 RVA: 0x00029DA4 File Offset: 0x00027FA4
		protected override byte[] PackCommand(byte[] data)
		{
			byte[] array = new byte[14 + data.Length];
			SoftBasic.HexStringToBytes("00 00 00 00 00 00 00 00 00 00 00 00 00 00").CopyTo(array, 0);
			bool flag = data.Length != 0;
			if (flag)
			{
				data.CopyTo(array, 14);
			}
			return array;
		}

		/// <summary>
		/// 重新开始接收数据
		/// </summary>
		/// <exception cref="T:System.ArgumentNullException"></exception>
		// Token: 0x060007D7 RID: 2007 RVA: 0x00029DE8 File Offset: 0x00027FE8
		private void RefreshReceive()
		{
			AppSession appSession = new AppSession();
			appSession.WorkSocket = this.CoreSocket;
			appSession.UdpEndPoint = new IPEndPoint(IPAddress.Any, 0);
			appSession.BytesContent = new byte[this.ReceiveCacheLength];
			this.CoreSocket.BeginReceiveFrom(appSession.BytesContent, 0, this.ReceiveCacheLength, SocketFlags.None, ref appSession.UdpEndPoint, new AsyncCallback(this.AsyncCallback), appSession);
		}

		// Token: 0x060007D8 RID: 2008 RVA: 0x00029E5C File Offset: 0x0002805C
		private void AsyncCallback(IAsyncResult ar)
		{
			AppSession appSession = ar.AsyncState as AppSession;
			bool flag = appSession != null;
			if (flag)
			{
				try
				{
					int num = appSession.WorkSocket.EndReceiveFrom(ar, ref appSession.UdpEndPoint);
					bool flag2 = !Authorization.asdniasnfaksndiqwhawfskhfaiw();
					if (flag2)
					{
						base.RemoveClient(appSession, "");
					}
					else
					{
						this.RefreshReceive();
						byte[] array = new byte[num];
						Array.Copy(appSession.BytesContent, 0, array, 0, num);
						ILogNet logNet = base.LogNet;
						if (logNet != null)
						{
							logNet.WriteDebug(this.ToString(), "Udp " + StringResources.Language.Receive + "：" + array.ToHexString(' '));
						}
						byte[] array2 = this.ReadFromFinsCore(array);
						bool flag3 = array2 != null;
						if (flag3)
						{
							appSession.WorkSocket.SendTo(array2, array2.Length, SocketFlags.None, appSession.UdpEndPoint);
							ILogNet logNet2 = base.LogNet;
							if (logNet2 != null)
							{
								logNet2.WriteDebug(this.ToString(), "Udp " + StringResources.Language.Send + "：" + array2.ToHexString(' '));
							}
							base.RaiseDataReceived(array);
						}
						else
						{
							base.RemoveClient(appSession, "");
						}
					}
				}
				catch (ObjectDisposedException)
				{
				}
				catch (Exception ex)
				{
					ILogNet logNet3 = base.LogNet;
					if (logNet3 != null)
					{
						logNet3.WriteException(this.ToString(), StringResources.Language.SocketEndReceiveException, ex);
					}
					this.RefreshReceive();
				}
				finally
				{
				}
			}
		}

		/// <inheritdoc />
		// Token: 0x060007D9 RID: 2009 RVA: 0x0002A020 File Offset: 0x00028220
		public override string ToString()
		{
			return string.Format("OmronFinsUdpServer[{0}]", base.Port);
		}
	}
}
